package com.hykj.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.hykj.toolsbean.DB;
import com.hykj.valuebean.IndexPic;

public class RightPicDao extends SuperDao {
	
	private DB mydb = null;
	
	public RightPicDao() {
		mydb = new DB();
	}
	
	public ArrayList<IndexPic> getAllIndexPic() throws SQLException {
		String sql = "select * from indexpic";
		Object[] params = null;
		ArrayList<IndexPic> list = new ArrayList<IndexPic>();
		mydb.doPstm(sql, params);
		ResultSet rs = mydb.getRs();
		while (rs.next()) {
			IndexPic indexPicIns = new IndexPic();
			indexPicIns.setId(rs.getInt(1));
			indexPicIns.setPicDir(rs.getString(2));
			indexPicIns.setPicHref(rs.getString(3));
			indexPicIns.setDescribes(rs.getString(4));
			list.add(indexPicIns);
		}
		rs.close();
		return list;
	}
	
	public String getPicDir(int id) throws SQLException {
		IndexPic indexPicIns = getIndexPic(id);
		return indexPicIns.getPicDir();
	}
	
	public String getPicHref(int id) throws SQLException {
		IndexPic indexPicIns = getIndexPic(id);
		return indexPicIns.getPicHref();
	}
	
	public String getDescribes(int id) throws SQLException {
		IndexPic indexPicIns = getIndexPic(id);
		return indexPicIns.getDescribes();
	}
	
	public IndexPic getIndexPic(int id) throws SQLException {
		String sql = "select * from indexpic where id = ?";
		Object[] params = {id};
		mydb.doPstm(sql, params);
		ResultSet rs = mydb.getRs();
		IndexPic indexPicIns = new IndexPic();
		while (rs.next()) {
			indexPicIns.setId(rs.getInt(1));
			indexPicIns.setPicDir(rs.getString(2));
			indexPicIns.setPicHref(rs.getString(3));
			indexPicIns.setDescribes(rs.getString(4));
		}
		rs.close();
		return indexPicIns;
		
	}
	
	public void updatePic(IndexPic indexPicIns) {
		String sql = "update indexpic set picDir = ?, picHref = ?, describes = ? where id = ?";
		Object[] params = {indexPicIns.getPicDir(), indexPicIns.getPicHref(), indexPicIns.getDescribes(), indexPicIns.getId()};
		mydb.doPstm(sql, params);
		mydb.closed();
	}
	
	public void deletePic(String url) {
		String sql = "delete from indexpic where id in" + url;
		Object[] params = null;
		mydb.doPstm(sql, params);
	}
	
	public void insertPic(IndexPic indexPicIns) {
		String sql = "insert indexpic (picDir, picHref, describes) values (?, ?, ?)";
		Object[] params = {indexPicIns.getPicDir(), indexPicIns.getPicHref(), indexPicIns.getDescribes()};
		mydb.doPstm(sql, params);
		
		
	}
	
	public void closed() {
		mydb.closed();
	}

}
